public class ext_OppSplitLost {
	
	Private Opportunity Oppty;
	List<Opportunity> oppyList = new List<Opportunity>();
	List<OppLineWrapper> oppLineList = new List<OppLineWrapper>();
	List<OpportunityLineItem> selectedWonOpps = new List<OpportunityLineItem>();
	List<OpportunityLineItem> selectedLostOpps = new List<OpportunityLineItem>();
	List<OpportunityLineItem> selectedPendingOpps = new List<OpportunityLineItem>();
	List<OpportunityLineItem> OppLineItemCopy = new List<OpportunityLineItem>();
    public ext_OppSplitLost(ApexPages.StandardController controller) {
	 this.Oppty = (Opportunity)controller.getRecord();
    }
	
	
	
	//use this or the stock code
	public List<OppLineWrapper> getOppLineItems() {
				oppLineList = new List<OppLineWrapper>();	
				for(OpportunityLineItem o : [SELECT Id, Line_Item_Status__c, Lost_Reason__c, OpportunityId, PricebookEntry.Name, Discount, Part_Number__c, 
				Description, UnitPrice, Quantity, Total_Amount_Plus_VAT__c, VAT_Value__c,VAT_Code__c,VAT_Rate__c,Commission__c,
				Freight_Rate__c,Duty__c, Margin__c, UOM__c, Delivery_Days__c, Inquiry_Part_No__c, Product_Group__c, 
				Product_Group_Description__c, Product_Group_Master__c, Item_Number__c, NL_Code__c, NL_Code_Description__c   
				 FROM OpportunityLineItem WHERE Lost_Reason__c = Null AND OpportunityId =: Oppty.id order by Item_Number__c ASC]) {
					oppLineList.add(new OppLineWrapper(o));
				}
			return oppLineList;

	}
	
	
	//get the selected values
	/*public PageReference getSelected() {
		selectedOpps.clear();
		for(OppLineWrapper oppwrapper : oppLineList) {
			if(oppwrapper.selected == true) {
				selectedOpps.add(oppwrapper.opi);
			}
		}
		return null;
	}
	*/
	
	public class OppLineWrapper {
		public OpportunityLineItem opi {get; set;}
		public Boolean selected {get; set;}
		public String status {get; set;}
		public OppLineWrapper(OpportunityLineItem opps) {
			opi = opps;
		}
	}
	
	public PageReference Next() {
		selectedWonOpps.clear();
		selectedLostOpps.clear();
		selectedPendingOpps.clear();
		
		PageReference opptyPage;
		
		try {
			system.debug('size of oppLineList to be processed by Next => ' + oppLineList.size());
			for(OppLineWrapper oppwrapper : oppLineList) {
					system.debug('check status value => ' + oppwrapper.opi.line_item_status__c + ' /id of => ' + oppwrapper.opi.id);
					//update lost reason for line items				
						selectedLostOpps.add(oppwrapper.opi);
							
			}
			//update	
			if(selectedLostOpps.size() > 0)		{
				update selectedLostOpps;
			}
	
			opptyPage = new ApexPages.StandardController(oppty).view();
	     	opptyPage.setRedirect(true);
		}
		catch (DmlException e)
		{
			ApexPages.addMessages(e);
		}
		return opptyPage;
	}
	
	
	
}